<template>
  <!-- eslint-disable -->
  <div class="doubleSelectionArr">
    <div class="topArea">
      <div class="inputSearch">
        <el-input v-model="doubleChooseParams.s" placeholder="请输入双选会名称关键词搜索" style="width: 280px;" prefix-icon="el-icon-search" clearable @keyup.enter.native="handleSearchTable" />
      </div>
      <div class="topBtnGroup">
        <el-button type="primary" size="small" icon="el-icon-plus" @click="handleEnterAddDoubleSelection">发布双选会</el-button>
      </div>
    </div>
    <div class="middleArea">
      <el-radio-group v-model="doubleChooseParams.status" @change="handleStatusCho">
        <el-radio-button :label="1">全部</el-radio-button>
        <el-radio-button :label="2">报名中</el-radio-button>
        <el-radio-button :label="3">报名截止</el-radio-button>
        <el-radio-button :label="4">进行中</el-radio-button>
        <el-radio-button :label="5">已结束</el-radio-button>
        <el-radio-button :label="6">准备中</el-radio-button>
      </el-radio-group>

      <div class="filter-container">
        <i class="el-icon-refresh-left refresh" @click="handleClearScreening"></i>
        <!-- <el-date-picker
          v-model="doubleChooseParams.start_apply_at"
          type="date"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择报名时间">
        </el-date-picker> -->
        <el-date-picker
          v-model="inSchoolTime"
          style="width: 400px;"
          type="daterange"
          range-separator="至"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始报名时间"
          end-placeholder="结束报名时间">
        </el-date-picker>
        <!-- <el-date-picker
          v-model="doubleChooseParams.meeting_start_at"
          type="date"
          value-format="yyyy-MM-dd HH:mm:ss"
          placeholder="请选择发布时间">
        </el-date-picker> -->
        <el-date-picker
          v-model="publishTime"
          style="width: 400px;"
          type="daterange"
          range-separator="至"
          value-format="yyyy-MM-dd HH:mm:ss"
          start-placeholder="开始发布时间"
          end-placeholder="结束发布时间">
        </el-date-picker>
        <el-button style="width:100px;margin-bottom: 20px;" @click="handleScreeningChange">筛选</el-button>
      </div>
    </div>

    <!-- 列表 -->
    <el-table :data="doubleSelectionList" v-loading="loading" style="width: 100%">
      <el-table-column align="center" prop="name" label="双选会名称"></el-table-column>
      <el-table-column align="center" label="报名起止时间">
        <template slot-scope="{row}">
          <div>{{ row.apply_start_at }}</div>
          <div>{{ row.apply_end_at }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="会议起止时间">
        <template slot-scope="{row}">
          <div>{{ row.meeting_start_at }}</div>
          <div>{{ row.meeting_end_at }}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="pass_company_count" label="参会企业数"></el-table-column>
      <el-table-column align="center" prop="status" label="状态">
        <template slot-scope="{row}">
          <span v-if="row.p_company_count == 0">{{ row.status }}</span>
          <el-badge v-else :value="row.p_company_count" style="marginTop: 10px; marginRight: 40px;">
            <span>{{ row.status }}</span>
          </el-badge>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="发布时间"></el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="{row}">
          <el-tooltip effect="dark" content="查看" placement="top-start">
            <span class="btnIcon view" @click="jumpIntoDetail(row)"></span>
          </el-tooltip>
          <el-tooltip v-show="row.status === '已结束'" effect="dark" content="删除" placement="top-start">
            <span class="btnIcon delete" @click="handleDeleteDoubleCho(row.id)"></span>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange" 
      @current-change="handleCurrentChange" 
      :current-page.sync="doubleChooseParams.page" 
      :page-sizes="[2,10,20,30,50]" 
      :page-size="doubleChooseParams.limit" 
      layout="prev, pager, next, total, sizes, jumper"
      :total="total"
      style="margin-top: 30px;"
    />
  </div>
</template>

<script>
import { getDoubleChooses, deleteDoubleCho } from '../../api/doubleChoose'
export default {
  data() {
    return {
      nowDate: new Date(),
      loading: false,
      doubleChooseParams: {
        page: 1,
        limit: 10,
        s: '', // 输入框搜索条件
        start_apply_at: null, // 开始报名
        end_apply_at: null, // 结束报名
        meeting_start_at: null, // 会议开始
        meeting_end_at: null, // 会议结束
        status: 1 // 状态值   1--全部   2--报名中  3--报名截止  4--进行中  5--已结束  6--准备中
      },
      doubleSelectionList: [],
      inSchoolTime: [],
      publishTime: [],
      total: null
    }
  },
  mounted() {
    this.getDoubleChooseList()
  },
  methods: {
    // 获取双选会列表
    getDoubleChooseList() {
      this.loading = true
      getDoubleChooses(this.doubleChooseParams).then(res => {
        for (const item of res.data) {
          const ASDate = new Date(Date.parse(item.apply_start_at))
          const AEDate = new Date(Date.parse(item.apply_end_at))
          const MSDate = new Date(Date.parse(item.meeting_start_at))
          const MEDate = new Date(Date.parse(item.meeting_end_at))
          if (this.nowDate < ASDate) {
            item.status = '准备中'
          } else if (ASDate <= this.nowDate && AEDate >= this.nowDate) {
            item.status = '报名中'
          } else if (AEDate < this.nowDate && MSDate > this.nowDate) {
            item.status = '报名截止'
          } else if (MSDate <= this.nowDate && MEDate >= this.nowDate) {
            item.status = '进行中'
          } else {
            item.status = '已结束'
          }
        }
        this.doubleSelectionList = res.data
        this.total = res.meta.total
      }).finally(() => {
        this.loading = false
      })
    },
    // 状态选择
    handleStatusCho() {
      this.doubleChooseParams.page = 1
      this.doubleChooseParams.limit = 10
      this.doubleChooseParams.s = ''
      this.doubleChooseParams.start_apply_at = null
      this.doubleChooseParams.meeting_start_at = null
      this.getDoubleChooseList()
    },
    // 分页
    handleSizeChange(val) {
      this.doubleChooseParams.limit = val
      this.getDoubleChooseList()
    },
    handleCurrentChange(val) {
      this.doubleChooseParams.page = val
      this.getDoubleChooseList()
    },
    // 点击'新增学生'按钮
    handleEnterAddDoubleSelection() {
      this.$router.push({ path: './add' })
    },
    handleEditDoubleSelection(row) {
      this.$router.push({
        path: './edit',
        query: {
          data: JSON.stringify(row)
        }
      })
    },
    // 点击'筛选'按钮
    handleScreeningChange() {
      this.doubleChooseParams.page = 1
      this.doubleChooseParams.limit = 10
      this.doubleChooseParams.status = 1
      this.doubleChooseParams.start_apply_at = this.inSchoolTime ? this.inSchoolTime[0] : ''
      this.doubleChooseParams.end_apply_at = this.inSchoolTime ? this.inSchoolTime[1] : ''
      this.doubleChooseParams.meeting_start_at = this.publishTime ? this.publishTime[0] : ''
      this.doubleChooseParams.meeting_end_at = this.publishTime ? this.publishTime[1] : ''
      this.getDoubleChooseList()
    },
    // 点击'清空'按钮
    handleClearScreening() {
      this.inSchoolTime = []
      this.publishTime = []
      this.doubleChooseParams.start_apply_at = ''
      this.doubleChooseParams.end_apply_at = ''
      this.doubleChooseParams.meeting_start_at = ''
      this.doubleChooseParams.meeting_end_at = ''
    },
    // 点击'搜索'按钮
    handleSearchTable() {
      this.doubleChooseParams.start_apply_at = ''
      this.doubleChooseParams.meeting_start_at = ''
      this.doubleChooseParams.page = 1
      this.doubleChooseParams.limit = 10
      this.doubleChooseParams.status = 1
      this.getDoubleChooseList()
    },
    // 删除双选会
    handleDeleteDoubleCho(id) {
      this.$confirm('确认删除该双选会信息', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        deleteDoubleCho(id).then(() => {
          this.$message({
            type: 'success',
            message: '操作成功！'
          })
          this.getDoubleChooseList()
        }).finally(() => {
          this.loading = false
        })
      })
    },
    // 查看详情
    jumpIntoDetail(row) {
      this.$router.push({
        path: './detail',
        query: {
          data: JSON.stringify(row)
        }
      })
    }
  }
}
</script>

<style lang="scss">
  $defaultColor: #0A2045;
  .doubleSelectionArr {
    padding: 30px;
    .topArea {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 30px;
      .el-input--suffix .el-input__inner {
        padding-right: 0;
      }
    }
    .middleArea {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 30px;
      .el-radio-button__orig-radio:checked+.el-radio-button__inner {
        background-color: #F1F7FF;
        border: none;
        color: #0C66FF;
        box-shadow: none;
      }
      .el-radio-button__inner {
        border: none;
        border-radius: 8px;
        color: #6C798F;
      }
      .el-radio-group {
        border: 1px solid #E1E4EB;
        border-radius: 8px;
      }

      .filter-container {
        .refresh {
          color: #B2B7C2;
          font-size: 20px;
          margin-right: 10px;
          cursor: pointer;
        }
        .inputWidth {
          margin-right: 10px;
          width: 224px;
          height: 38px;
          line-height: 38px;
        }
      }
    }
  }
  // 标题头样式修改
  .el-table {
    color: $defaultColor;
    font-size: 14px;
    line-height: 19px;
    thead {
      color: $defaultColor;
      tr, th {
        background: #FAFAFB;
      }
    }
  }
  .el-button:focus, .el-button:hover {
    color: #0C66FF;
    border-color: #0C66FF;
    background-color: #fff;
  }
  .el-button--primary {
    color: #FFF;
    background-color: #0C66FF;
    border-color: #0C66FF;
  }
  .el-button--small {
    border-radius: 8px;
  }
  .el-button--primary:hover {
    color: #fff;
    background-color: #0951CC;
    border-color: #0951CC;
  }
  .btnIcon {
    display: inline-block;
    width: 24px;
    height: 24px;
    cursor: pointer;
    margin-right: 10px;
  }
  // 删除
  .delete {
    background: url('../../assets/btn/icon_delete.png') no-repeat center center;
  }
  .delete:hover {
    background: url('../../assets/btn/icon_delete_s.png') no-repeat center center;
  }
  // 查看
  .view {
    background: url('../../assets/btn/icon_eye.png') no-repeat center center;
  }
  .view:hover {
    background: url('../../assets/btn/icon_eye_s.png') no-repeat center center;
  }
</style>
